package com.netty.example.nio;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.time.LocalDateTime;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class NIOClient {
    public static void main(String[] args) throws Exception {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        Selector selector = Selector.open();
        socketChannel.register(selector, SelectionKey.OP_CONNECT);
        socketChannel.connect(new InetSocketAddress(8899));
        while (true) {
            int selectNumber = selector.select();
            System.out.println("selectNumber: " + selectNumber);
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            for (SelectionKey selectionKey : selectionKeys) {
                if (selectionKey.isConnectable()) {
                    SocketChannel clientChannel = (SocketChannel) selectionKey.channel();
                    if (clientChannel.isConnectionPending()) {
                        clientChannel.finishConnect();
                        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
                        System.out.println(LocalDateTime.now() + " 连接成功");
                        byteBuffer.put((LocalDateTime.now() + " 连接成功").getBytes());
                        byteBuffer.flip();
                        clientChannel.write(byteBuffer);
                        ExecutorService executorService = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
                        executorService.submit(() -> {
                            while (true) {
                                try {
                                    byteBuffer.clear();
                                    InputStreamReader input = new InputStreamReader(System.in);
                                    BufferedReader br = new BufferedReader(input);
                                    String sendMessage = br.readLine();
                                    byteBuffer.put(sendMessage.getBytes());
                                    byteBuffer.flip();
                                    clientChannel.write(byteBuffer);
                                } catch (Exception e) {

                                }
                            }
                        });
                    }
                    clientChannel.register(selector, SelectionKey.OP_READ);
                } else if (selectionKey.isReadable()) {
                    SocketChannel clientChannel = (SocketChannel) selectionKey.channel();
                    ByteBuffer readBuffer = ByteBuffer.allocate(1024);
                    int readCount = clientChannel.read(readBuffer);
                    if (readCount > 0) {
                        String message = new String(readBuffer.array(), 0, readCount);
                        System.out.println(message);
                    }
                }
            }
            selectionKeys.clear();
        }
    }
}
